package tree.test563;

import listbyorder.utils.TreeNode;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/7/18 10:45
 */
public class Solution1 {

    public int findTilt(TreeNode root) {
        if (root == null) return 0;
        int l = sum(root.left);
        int r = sum(root.right);
        return Math.abs(l - r) + findTilt(root.left) + findTilt(root.right);
    }

    private int sum(TreeNode root) {
        if (root == null) return 0;
        int ans = root.val;
        return ans + sum(root.left) + sum(root.right);
    }
}
